//
//  SMLeftContentViewCtrl.swift
//  SmartMachine
//
//  Created by Sherman's iMac on 2019/11/6.
//  Copyright © 2019 Smart. All rights reserved.
//

import Cocoa
import SnapKit

class SMLeftContentViewCtrl : NSViewController, NSOutlineViewDataSource, NSOutlineViewDelegate {
    
    var platformBtn : SMButton!
    var basicBtn : SMButton!
    var outlineView : NSOutlineView!
    
    var treeRootNode : SMTreeModal! = {
        
        let rootNode = SMTreeModal.init(name: "root")
        rootNode.name = "我是总节点"
        
        let node_a : SMTreeModal! = SMTreeModal.init(name: "a")
        node_a.name = "A"
        let node_b : SMTreeModal! = SMTreeModal.init(name: "b")
        node_b.name = "B"
        let node_c : SMTreeModal! = SMTreeModal.init(name: "c")
        node_c.name = "C"
        
        var nodes_a : NSMutableArray? = NSMutableArray.init()
        var nodes_b : NSMutableArray? = NSMutableArray.init()
        var nodes_c : NSMutableArray? = NSMutableArray.init()
        
        node_a?.childNodes = nodes_a
        node_b?.childNodes = nodes_b
        node_c?.childNodes = nodes_c
        
        var nodes_root : NSMutableArray? = NSMutableArray.init()
        nodes_root?.add(node_a!)
        nodes_root?.add(node_b!)
        nodes_root?.add(node_c!)
        rootNode.childNodes = nodes_root
        return rootNode
    } ()
    
    
    override func loadView() {
        
        let contentView = NSView.init()
        contentView.wantsLayer = true
        //contentView.layer?.backgroundColor = FUNC_COLOR(r: 35, g: 47, b: 98).cgColor
        self.view = contentView
    }
    
    func configSubviews()  {
        
        platformBtn = SMButton.init(frame: NSZeroRect)
        //platformBtn.setButtonType(NSButton.ButtonType)
        platformBtn.bezelStyle = NSButton.BezelStyle.roundRect
        platformBtn.title = "平台配置"
        platformBtn.target = self
        platformBtn.action = #selector(clickPlatFormBtn)
        platformBtn.isBordered = true
//        platformBtn.wantsLayer = true
//        platformBtn.layer?.cornerRadius = 5.0
//        platformBtn.setBackgroundColor(curColor: NSColor.lightGray)
//        platformBtn.setMouseInBackgroundColor(curColor: NSColor.purple)
        
        basicBtn = SMButton.init(frame: NSZeroRect)
        basicBtn.title = "基础配置"
        basicBtn.bezelStyle = NSButton.BezelStyle.roundRect
        basicBtn.target = self
        basicBtn.action = #selector(clickBaseConfigBtn)
        basicBtn.isBordered = true
//        basicBtn.wantsLayer = true
//        basicBtn.layer?.cornerRadius = 5.0
//        basicBtn.setBackgroundColor(curColor: NSColor.lightGray)
//        basicBtn.setMouseInBackgroundColor(curColor: NSColor.purple)
        
        self.view.addSubview(platformBtn)
        self.view.addSubview(basicBtn)
        
        let distance = 20.0
        let btnH = 20.0
        
        platformBtn.snp.makeConstraints { (make) in
            
            make.left.equalToSuperview().offset(distance)
            make.right.equalToSuperview().offset(-distance)
            make.bottom.equalToSuperview().offset(-distance)
            make.height.equalTo(btnH)
        }
        
        basicBtn.snp.makeConstraints { (make) in
            
            make.height.equalTo(btnH)
            make.left.equalTo(platformBtn.snp_leftMargin)
            make.right.equalTo(platformBtn.snp_rightMargin)
            make.bottom.equalTo(platformBtn.snp.topMargin).offset(-distance - btnH)
       }
        
        outlineView = NSOutlineView.init()
        outlineView.headerView = nil
        outlineView.allowsColumnResizing = true
        outlineView.backgroundColor = NSColor.clear
//        outlineView.usesAlternatingRowBackgroundColors = true
        outlineView.delegate = self
        outlineView.dataSource = self
        outlineView.selectionHighlightStyle = NSTableView.SelectionHighlightStyle.none
        
        let column = NSTableColumn.init(identifier: NSUserInterfaceItemIdentifier(rawValue: "tree"))
        column.resizingMask = NSTableColumn.ResizingOptions.autoresizingMask
        outlineView.addTableColumn(column)
        
        let scrollV = NSScrollView.init()
        scrollV.hasHorizontalScroller = false
        scrollV.documentView = outlineView
        scrollV.drawsBackground = false
        view.addSubview(scrollV)
        
        scrollV.snp.makeConstraints { (make) in

            make.left.right.equalToSuperview();
            make.top.equalToSuperview().offset(distance / 2.0);
            make.bottom.equalToSuperview().offset(-3 * distance - 2 * btnH)
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        configSubviews()
    }
    
    override func viewDidAppear() {
        super.viewDidAppear()
        
        outlineView.expandItem(treeRootNode)
    }
    
    func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int {

        if item != nil && item is SMTreeModal{
            return (item as! SMTreeModal).childNodes!.count
        }
        return 1
    }
    
    func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any {
        
        if item == nil {
            return treeRootNode!
        }
        return (item as! SMTreeModal).childNodes![index]
    }
    
    func outlineView(_ outlineView: NSOutlineView, objectValueFor tableColumn: NSTableColumn?, byItem item: Any?) -> Any? {
        return item
    }
    
    func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {
        
        let modal : SMTreeModal! = item as? SMTreeModal
        let rowView = SMTreeTableRowView.init()
        rowView.titleLab.stringValue = modal.name ?? ""
        
        return rowView
    }
    
    func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool {
        
        let modal : SMTreeModal? = item as? SMTreeModal
        if modal == nil {
            return treeRootNode!.childNodes!.count > 0
        }
        return modal!.childNodes!.count > 0
    }
    
    func outlineView(_ outlineView: NSOutlineView, heightOfRowByItem item: Any) -> CGFloat {
        return 50.0
    }
    
    //mark Action
    @objc func clickPlatFormBtn(sender: NSButton) { 
        AppDelegate.appDelegate()?.showPlatformConfigCtrl()
    }
    
    //mark Action
    @objc func clickBaseConfigBtn(sender: NSButton) {
        AppDelegate.appDelegate()?.showBaseConfigCtrl()
    }  
    
}
